﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Util;
using System.Text.RegularExpressions;

namespace BibleBase
{
    [Serializable]
    class EntradaFinal
    {
        public int NVINumber { get; set; }
        public string NVITema { get; set; }
        public int RVNumber { get; set; }
        public string RVTema { get; set; }
    }

    [Serializable]
    public class EntradasFinal : PersistentObject
    {
        List<EntradaFinal> Entries = new List<EntradaFinal>();

        public void Add_A_B_C_Info(int rvNumber, string rvTema, string nviTema)
        {
            string rvTema1 = Repl(rvTema);
            string nviTema1 = Repl(nviTema);
            if (Entries.Any(x => x.RVNumber == rvNumber)) throw new Exception("Entrada repetida!");
            Entries.Add(new EntradaFinal { RVNumber = rvNumber, RVTema = rvTema1, NVITema = nviTema1 });
        }

        public void Add_D_E_Info(int nviNumber, string nviTema)
        {
            string nviTema1 = Repl(nviTema);
            EntradaFinal entry = Entries.FirstOrDefault(x => string.Compare(x.NVITema, nviTema1, true) == 0);
            if (entry == null) throw new Exception("Entrada no encontrada!");
            entry.NVINumber = nviNumber;
        }

        public int RVNumberFromNVINumber(int nviNumber)
        {
            EntradaFinal entry = Entries.FirstOrDefault(x => x.NVINumber == nviNumber);
            if (entry == null) throw new Exception("Entrada no encontrada!");
            return entry.RVNumber;
        }

        public static string DefaultPath { get { return Config.BinPath + @"\EntradasFinal.bin"; } }

        Regex rx = new Regex(@"(\(\w{1,3}\))+", RegexOptions.Compiled | RegexOptions.IgnoreCase);

        private string Repl(string text)
        {
            string result = text.Replace(",", "").Replace(" ", "");
            MatchCollection matches = rx.Matches(text);
            if (matches.Count > 0)
                foreach (Match match in matches)
                {
                    foreach (var group in match.Groups)
                    {
                        result = result.Replace(((Group)group).Value, "");
                    }
                }
            return result;
        }
    }
}
